package com.pengpeng.android.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.pengpeng.android.entity.AppEntity;
import com.pengpeng.android.entity.MapEntity;

public class AppDao extends SuperDao{
	
	
	private void createTable()
	{
		String sqlstr="create table app(";
		sqlstr+="appname varchar(20) not null,";
		sqlstr+="versionnum  int null";
		sqlstr+="version varchar(30) null,";
		sqlstr+="ifautoupdate varchar(2) null,";
		sqlstr+="language varchar(10) null,";
	    sqlstr+="constraint PK_APP primary key clustered (appname)";
	    sqlstr+=");";
		sqlitedb.execSQL(sqlstr);
	}//createTable
	
    private void insert(AppEntity appentity)
    {
    	String sqlstr="insert into app(";
    	sqlstr+="appname,versionnum,version,ifautoupdate,language) ";
    	sqlstr+="values('"+appentity.getAppname()+"','";
    	sqlstr+=appentity.getVersionnum()+"','";
    	sqlstr+=appentity.getVersion()+"','";
    	sqlstr+=appentity.getIfautoupdate()+"','";
    	sqlstr+=appentity.getLanguage()+"')";
		sqlitedb.execSQL(sqlstr);
    }//insert
    
    private void delete()
    {
    	//app的记录不能删除
    }//delete
    
    public void update(AppEntity appentity)
    {
    	String sqlstr="update app set ";
    	sqlstr+="appname='"+appentity.getAppname()+"',";
    	sqlstr+="versionnum='"+appentity.getVersionnum()+"',";
    	sqlstr+="version='"+appentity.getVersion()+"',";
    	sqlstr+="ifautoupdate='"+appentity.getIfautoupdate()+"',";
    	sqlstr+="language='"+appentity.getLanguage()+"'";
		sqlitedb.execSQL(sqlstr);

    	
    }//update
    
    public AppEntity query()
    {
    	String sqlstr="";
		Cursor cursor=null;
		int result=0;
		AppEntity appentity=null;
		sqlstr="select appname,versionnum,version,";
		sqlstr+="ifautoupdate,language from app";
		cursor=sqlitedb.rawQuery(sqlstr, null);
		cursor.moveToFirst();
		if(!cursor.isAfterLast())
		{
			String appname=cursor.getString(0);
			int versionnum=cursor.getInt(1);
			String version=cursor.getString(2);
			String ifautoupdate=cursor.getString(3);
			String language=cursor.getString(4);
			
			appentity=new AppEntity();
			appentity.setAppname(appname);
			appentity.setIfautoupdate(ifautoupdate);
			appentity.setLanguage(language);
			appentity.setVersion(version);
			appentity.setVersionnum(versionnum);
						
		}
		cursor.close();
        return appentity;
    }//query
    
    public void close()
    {
    	closeDatabase();
    }//close
    
    public AppDao()
    {
    	openDatabase();
    	if(!existTable("app"))
    	{
    		createTable();
    		AppEntity appentity=new AppEntity();
    		appentity.setAppname("PengPeng");
    		appentity.setIfautoupdate("1");
    		appentity.setLanguage("cn");
    		appentity.setVersion("1.0.0.20110726");
    		appentity.setVersionnum(0);
    		insert(appentity);
    	}
    }//AppDao
  
}
